import axios from 'axios';

// 默认测试用户配置
export const TEST_USER_CONFIG = {
  username: 'testuser001',
  password: '123456',
  phone: '13800138000',
  smsCode: '123456',
  nickName: '测试用户001',
  captchaId: '6886',
  verifyCode: '6886',
};

// AppUser类, 封装用户属性和操作
export class AppUser {
  username: string;
  password: string;
  phone: string;
  smsCode: string;
  nickName: string;
  captchaId: string;
  verifyCode: string;
  token: string = '';
  userId: number = -1;

  constructor(user: { username: string; password: string; phone: string; smsCode: string; nickName: string; captchaId: string; verifyCode: string }) {
    this.username = user.username;
    this.password = user.password;
    this.phone = user.phone;
    this.smsCode = user.smsCode;
    this.nickName = user.nickName;
    this.captchaId = user.captchaId;
    this.verifyCode = user.verifyCode;
  }

  async login() {
    console.log('login_ start');
    const res = await axios.post('http://localhost:8001/app/user/login/username', {
      username: this.username,
      password: this.password,
      captchaId: '0000',
      verifyCode: '0000',
    });
    if (res.status === 200 && res.data.code === 1000) {
      this.token = res.data.data.token;
      console.log('res.data.data.user', res.data.data.user);
      this.userId = res.data.data.user.id;
    }
    return res;
  }

  async register() {
    console.log('register_ start');
    const res = await axios.post('http://localhost:8001/app/user/login/register', {
      username: this.username,
      password: this.password,
      phone: this.phone,
      smsCode: this.smsCode,
      nickName: this.nickName,
    });
    if (res.status === 200 && res.data.code === 1000) {
      this.userId = res.data.data.id;
    }
    return res;
  }

  async logoff() {
    console.log('logoff_ start');
    if (!this.token) throw new Error('Token is empty, please login first.');
    const res = await axios.post(
      'http://localhost:8001/app/user/login/logoff',
      {},
      {
        headers: {
          Authorization: `${this.token}`,
        },
      }
    );
    return res;
  }

  async getInfo() {
    console.log('getInfo_ start');
    if (!this.userId) throw new Error('userId is empty, please login or register first.');
    const res = await axios.get(
      'http://localhost:8001/app/user/info/person',
      {
        headers: {
          Authorization: `${this.token}`,
        },
      }
    );
    return res;
  }
}

// 默认测试用户实例
export const testUser = new AppUser(TEST_USER_CONFIG); 